package com.catalog.ValueObject
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	[Bindable]
	public class PictureVO
	{
		public function PictureVO()
		{
		}
		
		private var _url:String;
		public function set url( value:String ):void
		{
			if( bitmap == null )
			{
				_url = value;
				readImage();
				
			}
		}
		
		public function get url():String
		{
			return _url;
		}
		
		
		public var name:String;
		
		public var isSaved:Boolean = true;
		
		private var _bitmap:Bitmap;

		public function get bitmap():Bitmap
		{
			return _bitmap;
		}

		public function set bitmap(value:Bitmap):void
		{
			_bitmap = value;
		}

		public function convertBytes(bytesArray:ByteArray):void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT,onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onErrorHandler);
			loader.loadBytes(bytesArray);
			
		}
		
		private function onLoadComplete(event:Event):void
		{
			var loader:LoaderInfo = event.currentTarget as LoaderInfo;
			var w:Number = loader.width;
			var h:Number = loader.height;
			var bitmapData:BitmapData = new BitmapData(w,h);
			bitmapData.draw(loader.content);
			bitmap = new Bitmap(bitmapData);
		}
		
		private function onErrorHandler( event:Event ):void
		{
			Alert.show("转换失败!");
		}
		
		private function readImage():void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT,onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onErrorHandler);
			loader.load(new URLRequest(url));
		}
	}
}